﻿@model PermissionViewModel



<script type="text/javascript">
    var setting = {
        view: {
            dblClickExpand: false
        },
        data: { simpleData: { enable: true }
        },
        check: {
            enable: true,
            chkboxType:  { "Y" : "ps", "N" : "ps" }
        }
    };


    $(document).ready(function(){

            @foreach(var sub in Model.SubSystems)
            {
                @:var data = @MvcHtmlString.Create(Model.Trees[sub.Code].ToJson());
                @:var zNodes = data.children;
                @:$.fn.zTree.init($("#Parentdropdownmenu_@sub.Code"), setting, zNodes);
            }

        });

    function SavePermission() {
        var permissionIds = new Array();
            @foreach (var sub in Model.SubSystems)
            {
                @:var treeObj_@sub.Code = $.fn.zTree.getZTreeObj("Parentdropdownmenu_@sub.Code");
                @:var nodes_@sub.Code = treeObj_@(sub.Code).getCheckedNodes(true);

                @:$.each(nodes_@sub.Code, function(index, item) { permissionIds.push(item.id); });
            }

        $.ajax({
                type: "POST",
            url: '@Url.Action("Save")',
            data: {
                    permissionIds:permissionIds,
                owner:"@Model.Owner",
                ownerId:"@Model.OwnerId"
            },
            dataType: "json",
            success: function (data) {
                    if (data.errors.length > 0) {
                        swal("出错啦！", data.errors, "error");
                    }
                    else {
                    $("#ajaxModal .modal").modal("toggle");
                        //ReloadDataTable();
                    }
                }
            });
        }
</script>
<div class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">关闭</span></button>
                <h4 class="modal-title">权限设置</h4>
            </div>
            <div class="modal-body" id="modalcontent">
                @if (!Model.SubSystems.Any())
                {
                    <div>未设置系统接入，无法正确显示权限。</div>
                }
                <ul class="nav nav-tabs">

                    @for (var i = 0; i < Model.SubSystems.Count; i++)
                    {
                        var sub = Model.SubSystems[i];
                        <li class="@(i==0?"active":"")"><a href="#tab_@sub.Code" data-toggle="tab">@sub.Name</a></li>
                    }
                </ul>

                <div class="tab-content">
                    @for (var i = 0; i < Model.SubSystems.Count; i++)
                    {
                        var sub = Model.SubSystems[i];
                        <div class="tab-pane @(i==0?"active":"")" id="tab_@sub.Code">
                            <ul id="Parentdropdownmenu_@sub.Code" class="ztree" style="margin-top: 0; width: 360px; margin-left: auto; margin-right: auto;"></ul>
                        </div>
                    }
                </div>
            </div>
            <div class="modal-footer">
                @Html.Bootstrap().Button("保存").Style(BootstrapStyle.Primary).Click("SavePermission();")
                @Html.Bootstrap().Button("关闭").Style(BootstrapStyle.Default).Attribute("data-dismiss", "modal")
            </div>
        </div>
    </div>
</div>